package book.spec;

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * 私钥贵方，用于还原私钥
 */
public class PKCS8EncodedKeySpecTest {

	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
		
		KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
		gen.initialize(512);
		KeyPair key = gen.generateKeyPair();
		
		byte[] privateKeyBytes = key.getPrivate().getEncoded();
		System.out.println(new BigInteger(privateKeyBytes));
		
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
		KeyFactory fac = KeyFactory.getInstance("dsa");
		PrivateKey key2 = fac.generatePrivate(keySpec);
		System.out.println(new BigInteger(key2.getEncoded()));
	}
}
